import {defineConfig, Plugin, type Connect} from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import {getChildBom, getMatPart, getSelectList, getSelectTcSFPurPartList} from "./mock/Maintenance.tsx"

// 创建自定义插件
const mockPlugin = (): Plugin => {
    return {
        name: 'mock-plugin',
        configureServer(server) {
            server.middlewares.use('/doc/repairUnit', (req, res, next) => {
                console.log(req.url, "xxxxxxxxxx---req.url")
                if (req.method === 'GET' && req.url?.startsWith('/selectTcSFPurPartList')) {
                    getSelectTcSFPurPartList(req, res)
                } else if (req.method === 'GET' && req.url?.startsWith('/selectList')) {
                    getSelectList(req, res)
                } else if (req.method === 'GET' && req.url?.startsWith('/childBom')) {
                    getChildBom(req, res)
                } else if (req.method === 'GET' && req.url?.startsWith('/getMatPart')) {
                    getMatPart(req, res)
                } else {
                    next()
                }
            })
        }
    }
}

export default defineConfig({
    build: {
        outDir: "build"
    },
    plugins: [
        react(),
        mockPlugin() // 添加自定义插件
    ],
    resolve: {
        alias: {
            '@src': path.resolve(__dirname, "./src"),
            '@core': path.resolve(__dirname, "./src/core")
        }
    },
    css: {
        preprocessorOptions: {
            less: {
                javascriptEnabled: true,
                modifyVars: {
                    hack: `true;@import (reference) "${path.resolve(__dirname, 'src/common.less')}"`
                }
            }
        }
    },
    server: {
        host: "0.0.0.0",
        port: 4300,
        proxy: {
            '/sf-drs': {
                target: 'http://192.168.31.202:9080/',
                changeOrigin: true,
            }
        }
    }
})